package cn.com.dycc.party_work.education_dept.service;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.party_work.education_dept.dao.EducationDeptDao;
import cn.com.dycc.party_work.education_dept.entity.EducationDept;
import cn.com.dycc.party_work.education_dept.entity.EducationDeptInfo;
import cn.com.dycc.party_work.education_dept.param.FindEducationDeptParam;
import cn.com.dycc.party_work.education_result.service.EduResultNotFoundException;
import cn.com.dycc.util.Paging;
/**
 * 
 * Description: 党员教育实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年6月27日 下午7:45:36.
 * Update Date Time: 
 * @see
 */
@Service("educationDeptService")
public class EducationDeptServiceImpl implements EducationDeptService{
	@Resource
	private EducationDeptDao educationDeptDao;
	
	@Resource(name="paging")
	private Paging paging;

	public int addEduDept(EducationDept eduDept, HttpServletRequest req) throws EduDeptNotFoundException {
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		if(user_id==null){
			throw new EduDeptNotFoundException("请重新登录！");
		}
		if(eduDept==null){
			throw new EduDeptNotFoundException("请填写任务和党支部");
		}
		if(eduDept.getEducation_id()==null||eduDept.getEducation_id().trim().isEmpty()){
			throw new EduDeptNotFoundException("请填写任务");
		}
		if(eduDept.getDept_id()==null||eduDept.getDept_id().trim().isEmpty()){
			throw new EduDeptNotFoundException("请填写党支部");
		}
		
		eduDept.setCreate_date(new Timestamp(new Date().getTime()));
		eduDept.setOpt_user_id(user_id);
		eduDept.setOrg_id((String)session.getAttribute("org_id"));
		int n = educationDeptDao.addEduDept(eduDept);
		if(n!=1){
			throw new EduDeptNotFoundException("添加失败");
		}		
		return 0;
	}

	public EducationDeptInfo findEduDeptById(String education_id, String dept_id, HttpServletRequest req)
			throws EduDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduDeptNotFoundException("请重新登录！");
		}
		if(education_id==null){
			throw new EduDeptNotFoundException("任务为空");
		}
		if(dept_id==null){
			throw new EduDeptNotFoundException("党支部为空");
		}
		EducationDeptInfo eduDept = educationDeptDao.findEduDeptById(education_id,dept_id,org_id);
		if(eduDept==null){
			throw new EduDeptNotFoundException("查询失败");
		}
		return eduDept;
	}

	public List<EducationDeptInfo> findAllEduDept(FindEducationDeptParam param, HttpServletRequest req)
			throws EduDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduDeptNotFoundException("请重新登录！");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);
		param.setOrg_id(org_id);
		List<EducationDeptInfo> list = educationDeptDao.findAllEduDept(param);
		
		return list;
	}

	public String findAllEduDeptCount(FindEducationDeptParam param, HttpServletRequest req)
			throws EduDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduDeptNotFoundException("重新登录");
		}
		param.setOrg_id(org_id);
		String str = educationDeptDao.findAllEduDeptCount(param);
		return str;
	}

	public int addAllEduDept(String education_id, String[] dept_ids, HttpServletRequest req)
			throws EduDeptNotFoundException {
		
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		if(user_id==null){
			throw new EduDeptNotFoundException("请重新登录！");
		}
		
		if(education_id==null||education_id.trim().isEmpty()){
			throw new EduDeptNotFoundException("请填写任务");
		}
		if(dept_ids==null||dept_ids.length<0){
			throw new EduDeptNotFoundException("请填写党支部");
		}
		List<EducationDept> list = new ArrayList<EducationDept>();
		EducationDept eduDept = null;
		for(int i=0;i<dept_ids.length;i++){
			eduDept = new EducationDept();
			eduDept.setEducation_id(education_id);
			eduDept.setDept_id(dept_ids[i]);
			eduDept.setCreate_date(new Timestamp(new Date().getTime()));
			eduDept.setOpt_user_id(user_id);
			eduDept.setOrg_id((String)session.getAttribute("org_id"));
			list.add(eduDept);
		}
		
		int n = educationDeptDao.addAllEduDept(list);
		if(n<=0){
			throw new EduDeptNotFoundException("添加失败");
		}
		return n;
	}

	public int deleteEduDept(String education_id, String dept_id, HttpServletRequest req)
			throws EduDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new EduDeptNotFoundException("请重新登录");
		}
		if(education_id==null||education_id.trim().isEmpty()){
			throw new EduDeptNotFoundException("请选择删除的教育学习");
		}
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new EduDeptNotFoundException("请选择删除的学习党支部");
		}
		
		int n = educationDeptDao.deleteEduDept(education_id,dept_id,org_id);
		if(n<1){
			throw new EduDeptNotFoundException("删除失败");
		}
		return n;
	}
	
	

}
